package prassein.rsm.web;

import java.io.File;
import java.util.Map;

import javax.servlet.ServletContext;

import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.apache.struts2.util.ServletContextAware;
import org.springframework.beans.factory.annotation.Autowired;

import prassein.rsm.common.utils.Constants;
import prassein.rsm.entity.base.Resume;
import prassein.rsm.entity.base.ResumeFile;
import prassein.rsm.service.base.ResumeFileManager;
import prassein.rsm.service.base.ResumeManager;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

@Results({ @Result(name = "success", location = "reloadReviewResume.jsp") })
public class DeleteAttachmentAction extends ActionSupport implements
ServletContextAware {

	private static final long serialVersionUID = -2438947313087703308L;

	@Autowired
	private ResumeManager resumeManager;
	
	@Autowired
	private ResumeFileManager resumeFileManager;
	
	private ServletContext context;
	
	private int order;
	
	public String execute() throws Exception {
		Map<String, Object> session = ActionContext.getContext().getSession();
		if (session.get(Constants.LATEST_EDIT_REUMEID) == null) {
			return ERROR;
		}
		int id = Integer.parseInt(session.get(Constants.LATEST_EDIT_REUMEID).toString());
		Resume resume = resumeManager.getResume(id);
		
		
		if (order == 1) {
			ResumeFile file = resumeFileManager.get(resume.getResumeFile1());
			if (file != null) {
				deleteFile(file.getLocation());
			}
			if (resume.getResumeFile2() != null) {
				resume.setResumeFile1(resume.getResumeFile2());
			} else {
				resume.setResumeFile1(-1);
			}
			if (resume.getResumeFile3() != null) {
				resume.setResumeFile2(resume.getResumeFile3());
			} else {
				resume.setResumeFile2(-1);
			}
			if (resume.getResumeFile4() != null) {
				resume.setResumeFile3(resume.getResumeFile4());
			} else {
				resume.setResumeFile3(-1);
			}
			resume.setResumeFile4(-1);
			resumeManager.save(resume);
		} else if (order == 2) {
			ResumeFile file = resumeFileManager.get(resume.getResumeFile2());
			if (file != null) {
				deleteFile(file.getLocation());
			}
			if (resume.getResumeFile3() != null) {
				resume.setResumeFile2(resume.getResumeFile3());
			} else {
				resume.setResumeFile2(-1);
			}
			if (resume.getResumeFile4() != null) {
				resume.setResumeFile3(resume.getResumeFile4());
			} else {
				resume.setResumeFile3(-1);
			}
			resume.setResumeFile4(-1);
			resumeManager.save(resume);
		} else if (order == 3) {
			ResumeFile file = resumeFileManager.get(resume.getResumeFile3());
			if (file != null) {
				deleteFile(file.getLocation());
			}
			if (resume.getResumeFile4() != null) {
				resume.setResumeFile3(resume.getResumeFile4());
			} else {
				resume.setResumeFile3(-1);
			}
			resume.setResumeFile4(-1);
			resumeManager.save(resume);
		} else if (order == 4) {
			ResumeFile file = resumeFileManager.get(resume.getResumeFile4());
			if (file != null) {
				deleteFile(file.getLocation());
			}
			resume.setResumeFile4(-1);
			resumeManager.save(resume);
		}
		return SUCCESS;
	}

	@Override
	public void setServletContext(ServletContext context) {
		this.context = context;
	}
	
	private void deleteFile(String loc) {
		File file = new File("webapps/resume/upload/" + loc);
		file.delete();
		
		file.getParentFile().delete();
	}

	public int getOrder() {
		return order;
	}

	public void setOrder(int order) {
		this.order = order;
	}

	public ResumeFileManager getResumeFileManager() {
		return resumeFileManager;
	}

	public void setResumeFileManager(ResumeFileManager resumeFileManager) {
		this.resumeFileManager = resumeFileManager;
	}
}
